/*
 * MainPanel.java
 *
 * Created on 31 de octubre de 2008, 11:04
 */
package mrs.gui;

import java.util.Vector;
import javax.swing.*;

import mrs.models.*;
import mrs.logging.*;
import mrs.ontology.*;

/**
 *
 * @author  pablo
 */
public class Scenario1Panel extends javax.swing.JPanel {

  private Guest guest = new Guest();

  /** Creates new form MainPanel */
  public Scenario1Panel() throws Exception {
    initComponents();
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelChooseIngredients = new javax.swing.JLabel();
        jLabelSuggestedRecipes = new javax.swing.JLabel();
        jPanelChooseIngredients = new javax.swing.JPanel();
        jLabelAllIngredients = new javax.swing.JLabel();
        jLabelMyIngredients = new javax.swing.JLabel();
        jScrollPaneAllIngredients = new javax.swing.JScrollPane();
        jListAllIngredients = new javax.swing.JList();
        jScrollPaneMyIngredients = new javax.swing.JScrollPane();
        jListMyIngredients = new javax.swing.JList();
        jButtonAddIngredient = new javax.swing.JButton();
        jButtonAddRemoveIngredient = new javax.swing.JButton();
        jPanelRecipes = new javax.swing.JPanel();
        jScrollPaneRecipes = new javax.swing.JScrollPane();
        jListRecipes = new javax.swing.JList();
        jPanelAddRestrictions = new javax.swing.JPanel();
        jCheckBoxUseAllIngredients = new javax.swing.JCheckBox();
        jCheckBoxDontBuyMoreIngredients = new javax.swing.JCheckBox();
        jLabelAddRestrictions = new javax.swing.JLabel();
        jButtonSuggestRecipes = new javax.swing.JButton();

        jLabelChooseIngredients.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabelChooseIngredients.setText("CHOOSE YOUR INGREDIENTS");

        jLabelSuggestedRecipes.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabelSuggestedRecipes.setText("SUGGESTED RECIPES");

        jPanelChooseIngredients.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabelAllIngredients.setText("ALL INGREDIENTS");

        jLabelMyIngredients.setText("MY INGREDIENTS");

        jListAllIngredients.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPaneAllIngredients.setViewportView(jListAllIngredients);

        jListMyIngredients.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPaneMyIngredients.setViewportView(jListMyIngredients);

        jButtonAddIngredient.setLabel("ADD SELECTED >>");
        jButtonAddIngredient.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonAddIngredientMouseClicked(evt);
            }
        });

        jButtonAddRemoveIngredient.setLabel("<< REMOVE SELECTED");
        jButtonAddRemoveIngredient.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonAddRemoveIngredientMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanelChooseIngredientsLayout = new javax.swing.GroupLayout(jPanelChooseIngredients);
        jPanelChooseIngredients.setLayout(jPanelChooseIngredientsLayout);
        jPanelChooseIngredientsLayout.setHorizontalGroup(
            jPanelChooseIngredientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelChooseIngredientsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelChooseIngredientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelChooseIngredientsLayout.createSequentialGroup()
                        .addGroup(jPanelChooseIngredientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelChooseIngredientsLayout.createSequentialGroup()
                                .addComponent(jLabelAllIngredients)
                                .addGap(104, 104, 104))
                            .addGroup(jPanelChooseIngredientsLayout.createSequentialGroup()
                                .addComponent(jScrollPaneAllIngredients, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanelChooseIngredientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelMyIngredients)
                            .addComponent(jScrollPaneMyIngredients, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelChooseIngredientsLayout.createSequentialGroup()
                        .addComponent(jButtonAddIngredient, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonAddRemoveIngredient, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanelChooseIngredientsLayout.setVerticalGroup(
            jPanelChooseIngredientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelChooseIngredientsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelChooseIngredientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelAllIngredients, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelMyIngredients))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelChooseIngredientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPaneMyIngredients, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                    .addComponent(jScrollPaneAllIngredients, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelChooseIngredientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAddIngredient)
                    .addComponent(jButtonAddRemoveIngredient))
                .addContainerGap())
        );

        jPanelRecipes.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jListRecipes.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPaneRecipes.setViewportView(jListRecipes);

        javax.swing.GroupLayout jPanelRecipesLayout = new javax.swing.GroupLayout(jPanelRecipes);
        jPanelRecipes.setLayout(jPanelRecipesLayout);
        jPanelRecipesLayout.setHorizontalGroup(
            jPanelRecipesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelRecipesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneRecipes, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelRecipesLayout.setVerticalGroup(
            jPanelRecipesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelRecipesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneRecipes, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanelAddRestrictions.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jCheckBoxUseAllIngredients.setText("I want to use ALL my ingredients");

        jCheckBoxDontBuyMoreIngredients.setText("I DON'T want to buy EXTRA ingredients");

        javax.swing.GroupLayout jPanelAddRestrictionsLayout = new javax.swing.GroupLayout(jPanelAddRestrictions);
        jPanelAddRestrictions.setLayout(jPanelAddRestrictionsLayout);
        jPanelAddRestrictionsLayout.setHorizontalGroup(
            jPanelAddRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAddRestrictionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelAddRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBoxUseAllIngredients)
                    .addComponent(jCheckBoxDontBuyMoreIngredients))
                .addContainerGap(187, Short.MAX_VALUE))
        );
        jPanelAddRestrictionsLayout.setVerticalGroup(
            jPanelAddRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAddRestrictionsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBoxUseAllIngredients)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBoxDontBuyMoreIngredients)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabelAddRestrictions.setFont(new java.awt.Font("Tahoma 12", 1, 12));
        jLabelAddRestrictions.setText("ADD RESTRICTIONS");

        jButtonSuggestRecipes.setText("SUGGEST RECIPES >>");
        jButtonSuggestRecipes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonSuggestRecipesMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanelAddRestrictions, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonSuggestRecipes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanelChooseIngredients, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelChooseIngredients, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelSuggestedRecipes)
                            .addComponent(jPanelRecipes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(39, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelAddRestrictions)
                        .addContainerGap(714, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelChooseIngredients)
                    .addComponent(jLabelSuggestedRecipes))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanelRecipes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelChooseIngredients, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jLabelAddRestrictions)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelAddRestrictions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonSuggestRecipes)
                .addContainerGap(59, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButtonAddRemoveIngredientMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonAddRemoveIngredientMouseClicked
  Ingredient selectedIngredient = (Ingredient) this.jListMyIngredients.getSelectedValue();

  guest.removeIngredient(selectedIngredient);

  this.jListMyIngredients = new JList(guest.getAvailableIngredientsVector());
  this.jListMyIngredients.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
  this.jListMyIngredients.setSelectedIndex(0);
  this.jScrollPaneMyIngredients.setViewportView(jListMyIngredients);
}//GEN-LAST:event_jButtonAddRemoveIngredientMouseClicked

private void jButtonSuggestRecipesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonSuggestRecipesMouseClicked
  // Clear Recipe list
  this.jListRecipes = new JList();
  this.jListRecipes.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
  this.jListRecipes.setSelectedIndex(0);
  this.jScrollPaneRecipes.setViewportView(jListRecipes);

  // Case 1: Recipes you can prepare using SOME of your available ingredients.
  //         You might also need to buy some MORE.
  if (!jCheckBoxUseAllIngredients.isSelected() && !jCheckBoxDontBuyMoreIngredients.isSelected()) {


    Logger.log.info("GUI detected Case 1: RecipesSomeIngredientsBuyMore");
    Logger.log.info("AVAILABLE INGREDIENTS:");
    for (Ingredient i : guest.getAvailableIngredientsSet()) {
      Logger.log.info(i.toString());
    }

    Vector<Recipe> suggestedRecipes = FoodOntology.RecipesSomeIngredientsBuyMore(guest.getAvailableIngredientsSet());

    // Recipe List

    this.jListRecipes = new JList(suggestedRecipes);
    this.jListRecipes.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    this.jListRecipes.setSelectedIndex(0);
    this.jScrollPaneRecipes.setViewportView(jListRecipes);

  }

  // Case 2: Recipes you can prepare using ALL your available ingredients.
  //         You might also need to buy some MORE.
  if (jCheckBoxUseAllIngredients.isSelected() && !jCheckBoxDontBuyMoreIngredients.isSelected()) {

    Logger.log.info("GUI detected Case 2: RecipesAllIngredientsBuyMore");
    Logger.log.info("AVAILABLE INGREDIENTS:");
    for (Ingredient i : guest.getAvailableIngredientsSet()) {
      Logger.log.info(i.toString());
    }

    Vector<Recipe> suggestedRecipes = FoodOntology.RecipesAllIngredientsBuyMore(guest.getAvailableIngredientsSet());

    // Recipe List

    this.jListRecipes = new JList(suggestedRecipes);
    this.jListRecipes.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    this.jListRecipes.setSelectedIndex(0);
    this.jScrollPaneRecipes.setViewportView(jListRecipes);

  }

  // Case 3: Recipes you can prepare with SOME of your available ingredientes.
  //         You don't need to buy any more.
  if (!jCheckBoxUseAllIngredients.isSelected() && jCheckBoxDontBuyMoreIngredients.isSelected()) {
    Logger.log.info("GUI detected Case 3: RecipesSomeIngredientsNoMore");
    Logger.log.info("AVAILABLE INGREDIENTS:");
    for (Ingredient i : guest.getAvailableIngredientsSet()) {
      Logger.log.info(i.toString());
    }

    Vector<Recipe> suggestedRecipes = FoodOntology.RecipesSomeIngredientsNoMore(guest.getAvailableIngredientsSet());

    // Recipe List

    this.jListRecipes = new JList(suggestedRecipes);
    this.jListRecipes.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    this.jListRecipes.setSelectedIndex(0);
    this.jScrollPaneRecipes.setViewportView(jListRecipes);
  }

  // Case 4: Recipes you can prepare using ALL your available ingredients.
  //         You don't need to buy any more.
  if (jCheckBoxUseAllIngredients.isSelected() && jCheckBoxDontBuyMoreIngredients.isSelected()) {
    Logger.log.info("GUI detected Case 4: RecipesAllIngredientsNoMore");
    Logger.log.info("AVAILABLE INGREDIENTS:");
    for (Ingredient i : guest.getAvailableIngredientsSet()) {
      Logger.log.info(i.toString());
    }

    Vector<Recipe> suggestedRecipes = FoodOntology.RecipesAllIngredientsNoMore(guest.getAvailableIngredientsSet());

    // Recipe List

    this.jListRecipes = new JList(suggestedRecipes);
    this.jListRecipes.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    this.jListRecipes.setSelectedIndex(0);
    this.jScrollPaneRecipes.setViewportView(jListRecipes);
  }
}//GEN-LAST:event_jButtonSuggestRecipesMouseClicked

private void jButtonAddIngredientMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonAddIngredientMouseClicked
  Ingredient selectedIngredient = (Ingredient) this.jListAllIngredients.getSelectedValue();

  //FIXME: Chapuza

  if (selectedIngredient != null) {
    guest.addIngredient(selectedIngredient);
    this.jListMyIngredients = new JList(guest.getAvailableIngredientsVector());
    this.jListMyIngredients.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    this.jListMyIngredients.setSelectedIndex(0);
    this.jScrollPaneMyIngredients.setViewportView(jListMyIngredients);
  }
}//GEN-LAST:event_jButtonAddIngredientMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddIngredient;
    private javax.swing.JButton jButtonAddRemoveIngredient;
    private javax.swing.JButton jButtonSuggestRecipes;
    private javax.swing.JCheckBox jCheckBoxDontBuyMoreIngredients;
    private javax.swing.JCheckBox jCheckBoxUseAllIngredients;
    private javax.swing.JLabel jLabelAddRestrictions;
    private javax.swing.JLabel jLabelAllIngredients;
    private javax.swing.JLabel jLabelChooseIngredients;
    private javax.swing.JLabel jLabelMyIngredients;
    private javax.swing.JLabel jLabelSuggestedRecipes;
    private javax.swing.JList jListAllIngredients;
    private javax.swing.JList jListMyIngredients;
    private javax.swing.JList jListRecipes;
    private javax.swing.JPanel jPanelAddRestrictions;
    private javax.swing.JPanel jPanelChooseIngredients;
    private javax.swing.JPanel jPanelRecipes;
    private javax.swing.JScrollPane jScrollPaneAllIngredients;
    private javax.swing.JScrollPane jScrollPaneMyIngredients;
    private javax.swing.JScrollPane jScrollPaneRecipes;
    // End of variables declaration//GEN-END:variables
  public void initGUI() throws Exception {
    Logger.log.debug("GUI init START");
    this.loadAllIngredientsGUI();
    this.loadMyIngredientsGUI();
    this.clearRecipeList();
    Logger.log.debug("GUI init END");
  }

  private void loadAllIngredientsGUI() throws Exception {
    Logger.log.debug("Load All Ingredients START");
    DefaultListModel allIngredientsListModel = new DefaultListModel();
    for (Ingredient i : Ingredient.getAll()) {
      allIngredientsListModel.addElement(i);
    }

    this.jListAllIngredients = new JList(allIngredientsListModel);
    this.jListAllIngredients.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    this.jListAllIngredients.setSelectedIndex(0);

    this.jScrollPaneAllIngredients.setViewportView(jListAllIngredients);
    Logger.log.debug("Load All Ingredients END");
  }

  private void loadMyIngredientsGUI() {

    this.jListMyIngredients = new JList(guest.getAvailableIngredientsVector());
    this.jListMyIngredients.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    this.jListMyIngredients.setSelectedIndex(0);

    this.jScrollPaneMyIngredients.setViewportView(jListMyIngredients);
  }

  private void clearRecipeList() {

    this.jListRecipes = new JList();
    this.jListRecipes.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    this.jListRecipes.setSelectedIndex(0);
    this.jScrollPaneRecipes.setViewportView(jListRecipes);
  }
}
